package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
//@Component
@Slf4j
public class SQLAnalyseAspect {
    //打印出所有查询sQL的执行时间，并且查询时间超过50ms的sQL，我们要打印出warning日志（慢sQL）
    @Around("execution(* com.itheima.mapper.*.select* (..))")
    public Object queryBeyondTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        long start = System.currentTimeMillis();
        Object proceed = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        long time = end - start;
        if (time>50){
            log.warn("----------慢SQL{}毫秒--------------", time);
        }else {
            log.info("----------执行时间是{}毫秒--------------", time);
        }
        return proceed;
    }

}
